package com.hqyj.controller;

import com.hqyj.common.ResponseResult;
import com.hqyj.pojo.TbEstateManager;
import com.hqyj.service.TbEstateManagerService;
import com.hqyj.util.JwtUtil;
import com.hqyj.util.RedisUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/estate")
public class EstateLoginController {

    @Autowired
    TbEstateManagerService tbEstateManagerService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 登录
     * 127.0.0.1:8080/estate/login ---- post
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestParam String username, @RequestParam String password){
        ResponseResult result = new ResponseResult();
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token = new UsernamePasswordToken(username, password);
        try{
            subject.login(token);
            String resToken = JwtUtil.sign(username);
            result.setData(result.new Token(resToken));
            redisUtil.set(resToken, username,30*60);
        } catch (Exception e){
            e.printStackTrace();
            result.setData("用户名或密码错误");
        }
        return result;
    }

}
